{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "import scipy.stats as stats # 统计模块\n",
    "import scipy\n",
    "# import pymysql  # 导入数据库模块\n",
    "from datetime import datetime # 时间模块\n",
    "import statsmodels.formula.api as smf  # OLS regression\n",
    "import statsmodels.api as sm\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "rawdata = pd.read_csv('Lecture 1 Data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age                      0\n",
       "gender                   0\n",
       "instalments_amount       0\n",
       "nominalrates             3\n",
       "tencentscore             0\n",
       "gaodescore               0\n",
       "highcontact              0\n",
       "deal                     0\n",
       "default               2795\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = rawdata[['age', 'gender','instalments_amount','nominalrates','tencentscore','gaodescore','highcontact','deal','default']]\n",
    "#检查是否有缺失值\n",
    "missing = data.isnull().sum()\n",
    "missing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见，利率、违约是否均有空缺值，考虑到数据的宝贵，对不同研究问题采用不同的子数据表进行回归分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age                     int64\n",
      "gender                   bool\n",
      "instalments_amount      int64\n",
      "nominalrates          float64\n",
      "tencentscore          float64\n",
      "gaodescore            float64\n",
      "highcontact              bool\n",
      "deal                    int64\n",
      "default                object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(data.dtypes)\n",
    "#注意 default一列数据格式是object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_24011/3535876502.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data['gender'] = data['gender'].astype(int)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_24011/3535876502.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data['highcontact'] = data['highcontact'].astype(int)\n"
     ]
    }
   ],
   "source": [
    "#数据格式转换 将是否赋值1-0\n",
    "data['gender'] = data['gender'].astype(int)\n",
    "data['highcontact'] = data['highcontact'].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.00000</td>\n",
       "      <td>4997.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>27.675400</td>\n",
       "      <td>0.146600</td>\n",
       "      <td>406201.42000</td>\n",
       "      <td>0.276058</td>\n",
       "      <td>58.608168</td>\n",
       "      <td>0.201975</td>\n",
       "      <td>0.492200</td>\n",
       "      <td>0.441400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.326146</td>\n",
       "      <td>0.353742</td>\n",
       "      <td>130623.36024</td>\n",
       "      <td>0.085912</td>\n",
       "      <td>14.218112</td>\n",
       "      <td>0.076724</td>\n",
       "      <td>0.499989</td>\n",
       "      <td>0.496604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>18.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>50000.00000</td>\n",
       "      <td>0.130080</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>0.023518</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>21.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>320000.00000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.888889</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>25.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>398000.00000</td>\n",
       "      <td>0.204579</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>32.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>498000.00000</td>\n",
       "      <td>0.359347</td>\n",
       "      <td>65.258929</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>56.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>869000.00000</td>\n",
       "      <td>0.494185</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>0.732120</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               age       gender  instalments_amount  nominalrates  \\\n",
       "count  5000.000000  5000.000000          5000.00000   4997.000000   \n",
       "mean     27.675400     0.146600        406201.42000      0.276058   \n",
       "std       8.326146     0.353742        130623.36024      0.085912   \n",
       "min      18.000000     0.000000         50000.00000      0.130080   \n",
       "25%      21.000000     0.000000        320000.00000      0.204560   \n",
       "50%      25.000000     0.000000        398000.00000      0.204579   \n",
       "75%      32.000000     0.000000        498000.00000      0.359347   \n",
       "max      56.000000     1.000000        869000.00000      0.494185   \n",
       "\n",
       "       tencentscore   gaodescore  highcontact         deal  \n",
       "count   5000.000000  5000.000000  5000.000000  5000.000000  \n",
       "mean      58.608168     0.201975     0.492200     0.441400  \n",
       "std       14.218112     0.076724     0.499989     0.496604  \n",
       "min        9.000000     0.023518     0.000000     0.000000  \n",
       "25%       53.888889     0.192094     0.000000     0.000000  \n",
       "50%       60.200000     0.192094     0.000000     0.000000  \n",
       "75%       65.258929     0.192094     1.000000     1.000000  \n",
       "max       98.000000     0.732120     1.000000     1.000000  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#描述性统计\n",
    "descriptive_stats = data[['age', 'gender','instalments_amount','nominalrates','tencentscore','gaodescore','highcontact','deal','default']].describe()\n",
    "descriptive_stats.to_excel('outcome1.1.xlsx', index=False)\n",
    "descriptive_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=data.dropna(subset=['nominalrates'])\n",
    "#对因变量不是default的情况，用data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_24011/657454543.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data['default'] = reg_data['default'].astype(int)\n"
     ]
    }
   ],
   "source": [
    "#因为default这一行的数据很特殊，它有缺失值，并且很大，但是数据是很宝贵的，为此本文专门对default数据进行特殊处理\n",
    "reg_data=data.dropna(subset=['default'])\n",
    "reg_data['default'] = reg_data['default'].astype(int)\n",
    "#对因变量是逾期，自变量不包括买家信用的，用reg_data,即第二问第二、三次回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2203.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.419882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.493651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           default\n",
       "count  2203.000000\n",
       "mean      0.419882\n",
       "std       0.493651\n",
       "min       0.000000\n",
       "25%       0.000000\n",
       "50%       0.000000\n",
       "75%       1.000000\n",
       "max       1.000000"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "descriptive_stats2 = reg_data[['default']].describe()\n",
    "descriptive_stats2.to_excel('outcome1.2.xlsx', index=False)\n",
    "descriptive_stats2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.677831\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2201\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.003561\n",
      "Time:                        14:11:11   Log-Likelihood:                -1493.3\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                  0.001087\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -0.8265      0.161     -5.123      0.000      -1.143      -0.510\n",
      "tencentscore     0.0091      0.003      3.250      0.001       0.004       0.015\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "#未加控制变量\n",
    "X1 = sm.add_constant(reg_data['tencentscore'])  \n",
    "y1 = reg_data['default'] \n",
    "logit_model1 = sm.Logit(y1, X1)\n",
    "result1 = logit_model1.fit()\n",
    "print(result1.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_24011/875144610.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data['ln_amount'] = np.log(reg_data['instalments_amount'])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.675350\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2198\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.007210\n",
      "Time:                        14:11:11   Log-Likelihood:                -1487.8\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0002398\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -4.3267      1.646     -2.629      0.009      -7.552      -1.101\n",
      "tencentscore     0.0095      0.003      3.361      0.001       0.004       0.015\n",
      "gender          -0.0016      0.114     -0.014      0.989      -0.224       0.221\n",
      "ln_amount        0.2386      0.126      1.896      0.058      -0.008       0.485\n",
      "nominalrates     1.4636      0.537      2.727      0.006       0.412       2.516\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "#加入控制变量\n",
    "reg_data['ln_amount'] = np.log(reg_data['instalments_amount'])\n",
    "X1_1 = reg_data[['tencentscore', 'gender', 'ln_amount','nominalrates']]\n",
    "y1_1 = reg_data['default']\n",
    "X1_1 = sm.add_constant(X1_1)\n",
    "logit_model = sm.Logit(y1_1, X1_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.677694\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2201\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.003763\n",
      "Time:                        14:11:11   Log-Likelihood:                -1493.0\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0007836\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const         -0.7071      0.123     -5.748      0.000      -0.948      -0.466\n",
      "gaodescore     1.9828      0.593      3.342      0.001       0.820       3.146\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "X2 = sm.add_constant(reg_data['gaodescore']) \n",
    "y1 = reg_data['default']\n",
    "logit_model1 = sm.Logit(y1, X2)\n",
    "result2 = logit_model1.fit()\n",
    "print(result2.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.675476\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2198\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.007023\n",
      "Time:                        14:11:11   Log-Likelihood:                -1488.1\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0003095\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -3.8689      1.624     -2.382      0.017      -7.052      -0.685\n",
      "gaodescore       1.9552      0.596      3.281      0.001       0.787       3.123\n",
      "gender           0.0396      0.113      0.349      0.727      -0.183       0.262\n",
      "ln_amount        0.2153      0.125      1.719      0.086      -0.030       0.461\n",
      "nominalrates     1.3985      0.537      2.604      0.009       0.346       2.451\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X2_1 = reg_data[['gaodescore', 'gender', 'ln_amount','nominalrates']]\n",
    "y2_1 = reg_data['default']\n",
    "X2_1 = sm.add_constant(X2_1)\n",
    "logit_model = sm.Logit(y2_1, X2_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.663147\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                 0.03364\n",
      "Time:                        14:11:11   Log-Likelihood:                -3313.7\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 4.170e-52\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            1.6089      0.130     12.412      0.000       1.355       1.863\n",
      "tencentscore    -0.0316      0.002    -14.585      0.000      -0.036      -0.027\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X3 = sm.add_constant(data['tencentscore'])\n",
    "y2 = data['deal'] \n",
    "logit_model3 = sm.Logit(y2, X3)\n",
    "result3 = logit_model3.fit()\n",
    "print(result3.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.646681\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                 0.05763\n",
      "Time:                        14:11:12   Log-Likelihood:                -3231.5\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 2.930e-84\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            8.2179      1.102      7.458      0.000       6.058      10.378\n",
      "tencentscore    -0.0332      0.002    -15.024      0.000      -0.038      -0.029\n",
      "gender           0.4823      0.084      5.772      0.000       0.318       0.646\n",
      "ln_amount       -0.5803      0.084     -6.900      0.000      -0.745      -0.416\n",
      "nominalrates     3.1476      0.345      9.118      0.000       2.471       3.824\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "data['ln_amount'] = np.log(data['instalments_amount'])\n",
    "X3_1 = data[['tencentscore', 'gender', 'ln_amount','nominalrates']]\n",
    "y3_1 = data['deal']\n",
    "X3_1 = sm.add_constant(X3_1)\n",
    "logit_model = sm.Logit(y3_1, X3_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.680475\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.008388\n",
      "Time:                        14:11:12   Log-Likelihood:                -3400.3\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 3.331e-14\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.3518      0.084      4.176      0.000       0.187       0.517\n",
      "gaodescore    -2.9315      0.399     -7.353      0.000      -3.713      -2.150\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "X4 = sm.add_constant(data['gaodescore']) \n",
    "y2 = data['deal'] \n",
    "logit_model4 = sm.Logit(y2, X4)\n",
    "result4 = logit_model4.fit()\n",
    "print(result4.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.666047\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                 0.02941\n",
      "Time:                        14:11:12   Log-Likelihood:                -3328.2\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 1.600e-42\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            5.7604      1.061      5.428      0.000       3.680       7.841\n",
      "gaodescore      -2.8402      0.406     -7.000      0.000      -3.635      -2.045\n",
      "gender           0.4190      0.082      5.121      0.000       0.259       0.579\n",
      "ln_amount       -0.4927      0.082     -6.008      0.000      -0.653      -0.332\n",
      "nominalrates     3.0468      0.338      9.005      0.000       2.384       3.710\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X4_1 = data[['gaodescore', 'gender', 'ln_amount','nominalrates']]\n",
    "y4_1 = data['deal']\n",
    "X4_1 = sm.add_constant(X4_1)\n",
    "logit_model = sm.Logit(y4_1, X4_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.685351\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                0.001283\n",
      "Time:                        14:11:12   Log-Likelihood:                -3424.7\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                  0.003014\n",
      "===============================================================================\n",
      "                  coef    std err          z      P>|z|      [0.025      0.975]\n",
      "-------------------------------------------------------------------------------\n",
      "const          -0.3197      0.040     -7.949      0.000      -0.399      -0.241\n",
      "highcontact     0.1691      0.057      2.965      0.003       0.057       0.281\n",
      "===============================================================================\n"
     ]
    }
   ],
   "source": [
    "X5 = sm.add_constant(data['highcontact'])  \n",
    "y3 = data['deal'] \n",
    "logit_model5 = sm.Logit(y3, X5)\n",
    "result5 = logit_model5.fit()\n",
    "print(result5.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.670371\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                 0.02311\n",
      "Time:                        14:11:12   Log-Likelihood:                -3349.8\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 3.050e-33\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            5.0351      1.051      4.791      0.000       2.975       7.095\n",
      "highcontact      0.1719      0.058      2.967      0.003       0.058       0.285\n",
      "gender           0.4395      0.082      5.389      0.000       0.280       0.599\n",
      "ln_amount       -0.4883      0.082     -5.983      0.000      -0.648      -0.328\n",
      "nominalrates     3.0890      0.337      9.156      0.000       2.428       3.750\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X6_1 = data[['highcontact', 'gender', 'ln_amount','nominalrates']]\n",
    "y6_1 = data['deal']\n",
    "X6_1 = sm.add_constant(X6_1)\n",
    "logit_model = sm.Logit(y6_1, X6_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:anaconda3]",
   "language": "python",
   "name": "conda-env-anaconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
